package com.taoge.p4.common.rabbit.producer;

import com.alibaba.fastjson.JSON;
import com.taoge.p4.common.rabbit.exchange.RabbitmqExchange;
import com.taoge.p4.common.rabbit.msg.BaseMqMsg;
import com.taoge.p4.common.rabbit.routingKey.BrandRoutingKey;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class RabbitmqProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     * @param exchange 交换机
     * @param routingKey 路由键
     * @param message 消息
     */
    public <T extends BaseMqMsg>  void send(RabbitmqExchange exchange, BrandRoutingKey routingKey, T message) {
        rabbitTemplate.convertAndSend(exchange.name(), routingKey.getKey(), JSON.toJSONString(message));
    }
}
